<!-- 新增编辑车辆 -->
<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      :visible="dialogFormVisible"
      width="600px"
      @close="closeDailog"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-position="right"
        label-width="110px"
        style="width: 100%"
      >
        <el-form-item
          label="车辆类型:"
          prop="truckTypeId"
        >
          <el-select
            v-model="form.truckTypeId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in carTypeList"
              :key="item.id"
              ref="form"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="licensePlate"
          label="车牌号码："
        >
          <el-input
            v-model="form.licensePlate"
            placeholder="请输入车牌号码"
          />
        </el-form-item>
        <el-form-item
          prop="deviceGpsId"
          label="GPS设备ID："
        >
          <el-input
            v-model="form.deviceGpsId"
            placeholder="请输入车牌号码"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="text-align: center"
      >
        <el-button
          class="save-btn"
          type="warning"
          @click="addCar"
        >确认</el-button>
        <el-button
          class="cancel-btn"
          @click="closeDailog"
        >取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { truckTypeAdd } from '@/api/transit'
import { Message } from 'element-ui'
export default {
  name: 'VehicleAdd',

  props: {
    dialogFormVisible: {
      type: Boolean,
      required: true
    },
    carTypeList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      form: {
        licensePlate: null,
        deviceGpsId: null,
        truckTypeId: null
      },
      rules: {
        licensePlate: [
          { required: true, message: '车牌号码不能为空', trigger: 'blur' },

          { pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/, message: '车牌号码格式不正确', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: '车辆GPS设备ID不能为空', trigger: 'blur' }
        ],
        truckTypeId: [
          { required: true, message: '车辆类型不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    /**
     * 添加车辆
     */
    addCar() {
      // 1.兜底效验
      this.$refs.form.validate(async key => {
        if (!key) return
        // 2.调用接口
        const res = await truckTypeAdd(this.form)
        if (res.code === 200) {
        // 3.提示成功
          Message.success('添加成功')
          // 4.关闭弹窗
          this.closeDailog()
          // 5.刷新页面
          this.$parent.getVehicleList()
        } else {
          Message.error('新增失败')
        }
      })
    },
    /**
     * 关闭模态框
     */
    closeDailog() {
      this.$emit('update:dialogFormVisible', false)
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style scoped lang="scss">
/deep/ .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
/deep/ .el-dialog__header{
height: 50px;
}
</style>
